package com.kzd.gtboon.main.home.adapter

import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ColorUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.gtboon.R
import com.kzd.gtboon.entity.GameCoupon
import com.kzd.gtboon.entity.WelfareCoupon
import com.kzd.gtboon.main.game.adapter.GameListLabelAdapter
import com.kzd.gtboon.main.toGameDetailsActivity
import com.module.lemlin.image.loadImage
import java.lang.Exception
import java.lang.StringBuilder

class CouponEnjoyAdapter :
    BaseQuickAdapter<GameCoupon, BaseViewHolder>(R.layout.item_game_coupon_enjoy) {

    init {
        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            toGameDetailsActivity(itemData.id)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GameCoupon) {
        holder.setText(R.id.tvGameCouponEnjoyName, item.name)
            .setText(R.id.tvGameCouponEnjoyType, "${item.type} · ${item.size}MB")
            .setText(R.id.tvGameCouponEnjoyPrice, "￥${item.all_money}")
            .setText(R.id.tvGameCouponEnjoyNum, "共${item.count}张券")
            .getView<ImageView>(R.id.ivGameCouponEnjoyIcon)
            .loadImage(src = item.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)
    }
}

class CouponAreaAdapter :
    BaseQuickAdapter<GameCoupon, BaseViewHolder>(R.layout.item_game_coupon_area) {

    init {
        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            toGameDetailsActivity(itemData.id, index = 1)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GameCoupon) {
        holder.setText(R.id.tvGameCouponName, item.name)
            .setText(R.id.tvGameCouponType, "${item.type} · ${item.size}MB")
            .setText(R.id.tvGameCouponPrice, "￥${item.all_money}")
            .setText(R.id.tvGameCouponNum, "共${item.count}张券")
            .getView<ImageView>(R.id.ivGameCouponIcon)
            .loadImage(src = item.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)
        val rvHomeType2Body = holder.getView<RecyclerView>(R.id.rvGameCouponBody)
        val layoutManager = LinearLayoutManager(context).apply {
            orientation = LinearLayoutManager.HORIZONTAL
        }
        rvHomeType2Body.layoutManager = layoutManager
        rvHomeType2Body.adapter = GameListLabelAdapter().apply {
            setNewInstance(item.biaoqian.toMutableList())
        }
    }
}


class CouponPriceAdapter(val action: Action) :
    BaseQuickAdapter<WelfareCoupon, BaseViewHolder>(R.layout.item_coupon_price) {

    enum class Action {
        COUPON_PRICE_DRAW, COUPON_PRICE_USE
    }

    init {
        addChildClickViewIds(R.id.tvCouponBottomDraw, R.id.tvCouponBottomUse)
    }

    override fun convert(holder: BaseViewHolder, item: WelfareCoupon) {

        holder.setText(R.id.tvCouponBottomPrice, item.jian)
            .setText(
                R.id.tvCouponBottomPriceFull, try {
                    if (item.money.toInt() == 0)
                        "任意金额" else "满${item.money}元可用"
                } catch (e: Exception) {
                    "任意金额"
                }
            )
            .setText(R.id.tvCouponBottomTime, item.end_str)
            .setText(
                R.id.tvCouponBottomDesc,
                StringBuilder().append("仅限《").append(item.game_name).append("》充值使用")
            )

        holder.getView<TextView>(R.id.tvCouponBottomName).apply {
            text = item.name
            val color = if (action == Action.COUPON_PRICE_USE) {
                when (item.status) {
                    0 -> R.color.black
                    else -> R.color.FF_999999
                }
            } else {
                when (item.is_draw) {
                    true -> R.color.black
                    else -> R.color.FF_999999
                }
            }
            setTextColor(ColorUtils.getColor(color))
        }
        holder.getView<TextView>(R.id.tvCouponBottomUse).apply {
            val color = if (action == Action.COUPON_PRICE_USE) {
                when (item.status) {
                    0 -> R.color.FF_F45444
                    else -> R.color.FF_999999
                }
            } else {
                when (item.is_draw) {
                    true -> R.color.FF_F45444
                    else -> R.color.FF_999999
                }
            }
            setTextColor(ColorUtils.getColor(color))
        }
        holder.getView<TextView>(R.id.tvCouponBottomDraw).apply {
            text = if (action == Action.COUPON_PRICE_USE) {
                when (item.status) {
                    0 -> "去\n使\n用"
                    1 -> "已\n使\n用"
                    else -> "已\n过\n期"
                }
            } else {
                if (item.is_draw) "立\n即\n领\n取" else "已\n领\n取"
            }


            val color = if (action == Action.COUPON_PRICE_USE) {
                when (item.status) {
                    0 -> R.color.FF_F45444
                    else -> R.color.FF_999999
                }
            } else {
                when (item.is_draw) {
                    true -> R.color.FF_F45444
                    else -> R.color.FF_999999
                }
            }
            setTextColor(ColorUtils.getColor(color))
        }

        holder.getView<LinearLayout>(R.id.llCouponBottomBg).apply {
            if (action == Action.COUPON_PRICE_USE) {
                if (item.status == 0) {
                    setBackgroundResource(R.mipmap.djj_wlq_bg)
                } else {
                    setBackgroundResource(R.mipmap.djj_kb_icon)
                }
            } else {
                if (item.is_draw) {
                    setBackgroundResource(R.mipmap.djj_wlq_bg)
                } else {
                    setBackgroundResource(R.mipmap.djj_kb_icon)
                }
            }
        }
    }
}